package info.xingxingdd.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import info.xingxingdd.admin.common.PageQuery;
import info.xingxingdd.admin.common.Result;
import info.xingxingdd.admin.entity.ClientAppInfo;
import info.xingxingdd.admin.service.ClientAppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 客户端app信息控制器
 */
@RestController
@RequestMapping("/api/app/info")
public class ClientAppInfoController {
    
    @Autowired
    private ClientAppInfoService clientAppInfoService;
    
    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result<IPage<ClientAppInfo>> page(@RequestBody PageQuery query) {
        LambdaQueryWrapper<ClientAppInfo> wrapper = new LambdaQueryWrapper<>();
        
        // 关键字查询
        if (StringUtils.hasText(query.getKeyword())) {
            wrapper.like(ClientAppInfo::getAppName, query.getKeyword())
                   .or()
                   .like(ClientAppInfo::getAppKey, query.getKeyword())
                   .or()
                   .like(ClientAppInfo::getDescription, query.getKeyword());
        }
        
        // 按创建时间倒序排序
        wrapper.orderByDesc(ClientAppInfo::getCreateAt);
        
        Page<ClientAppInfo> page = new Page<>(query.getCurrent(), query.getSize());
        IPage<ClientAppInfo> result = clientAppInfoService.page(page, wrapper);
        
        return Result.success(result);
    }
    
    /**
     * 根据类别查询
     */
    @GetMapping("/list/{categoryKey}")
    public Result<List<ClientAppInfo>> listByCategoryKey(@PathVariable String categoryKey) {
        LambdaQueryWrapper<ClientAppInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClientAppInfo::getCategoryKey, categoryKey);
        wrapper.orderByDesc(ClientAppInfo::getCreateAt);
        List<ClientAppInfo> list = clientAppInfoService.list(wrapper);
        return Result.success(list);
    }
    
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result<ClientAppInfo> getById(@PathVariable Integer id) {
        ClientAppInfo appInfo = clientAppInfoService.getById(id);
        return Result.success(appInfo);
    }
    
    /**
     * 新增
     */
    @PostMapping
    public Result<ClientAppInfo> save(@RequestBody ClientAppInfo appInfo) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        appInfo.setCreateAt(now);
        appInfo.setUpdateAt(now);
        
        clientAppInfoService.save(appInfo);
        return Result.success(appInfo);
    }
    
    /**
     * 修改
     */
    @PutMapping
    public Result<ClientAppInfo> update(@RequestBody ClientAppInfo appInfo) {
        // 设置更新时间
        appInfo.setUpdateAt(LocalDateTime.now());
        
        clientAppInfoService.updateById(appInfo);
        return Result.success(appInfo);
    }
    
    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        clientAppInfoService.removeById(id);
        return Result.success();
    }
} 